#include <iostream>
#include "spline.h"
#include <cmath>
#include <vector>
#include <algorithm>
#include <fstream>

class Fun : public Function
{
public:
    double operator()(double _x)
    {
        return 1.0/(1.0 + _x*_x);
    }
    double diff(double _x)
    {
        return -2.0*_x/pow(1 + _x*_x, 2);
    }
    double diff_2(double _x)
    {
        return (6.0*_x*_x - 2)/pow(1.0 + _x*_x, 3);
    }
};

int main()
{
    std::ofstream fout_3, fout_2, fout_1, fout_error_1, fout_error_3, fout_error;//3表示阶为3的
    fout_3.open("C_3.txt");
    fout_2.open("C_2.txt");
    fout_1.open("C_1.txt");
    fout_error_1.open("D_error1.txt");
    fout_error_3.open("D_error3.txt");
    fout_error.open("D_error.txt");
    int N_3=11;
    int N_2=10;
    double x[7] ={-3.5, -3, -0.5, 0, 0.5, 3, 3.5}; 
    double left_3 = -5;
    double right_3 = 5;
    double left_2 = -4.5;
    double right_2 = 4.5;
    Fun f;
    std::vector<double> Point_3;
    std::vector<double> Point_2;
    for(int i = 1; i <= N_3; i++)
    {
        Point_3.push_back(-6+i);
    }
    for(int i = 1; i <= N_2; i++)
    {
       Point_2.push_back(i-11.0/2);
    }
    B_spline_interpolation B_3(f, Point_3, 1, 3);//完全三次样条way=1
    B_spline_interpolation B_2(f, Point_2, 1, 2);
    B_spline_interpolation B_1(f, Point_2, 1, 1);
    B_3.solve();
    B_2.solve();
    B_1.solve();
    for(int m = 0; m < 1000; m++)
    {
        fout_3 << B_3(left_3 + m*0.01) << std::endl;
        fout_error_3 << fabs(B_3(left_3 + m*0.01)-f(left_3 + m*0.01)) << std::endl;
    }
    for(int m = 0; m < 900; m++)
    {
       fout_2 << B_2(left_2 + m*0.01) << std::endl;
    }
    for(int m = 0; m < 900; m++)
    {
       fout_1 << B_1(left_2 + m*0.01) << std::endl;
       fout_error_1 << fabs(B_1(left_2 + m*0.01)-f(left_2 + m*0.01)) << std::endl;
    }
    fout_error << "一次样条插值误差：" << std::endl;
    for(int i = 0; i < 7; i++)
    {
        fout_error << fabs(B_1(x[i])-f(x[i])) << std::endl;
    }
    fout_error << "三次样条插值误差：" << std::endl;
    for(int i = 0; i < 7; i++)
    {
        fout_error << fabs(B_3(x[i])-f(x[i])) << std::endl;
    }
    fout_3.close();
    fout_2.close();
    fout_1.close();
    fout_error_1.close();
    fout_error_3.close();
    fout_error.close();
    return 0;
}